<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-02 16:09:55
 * @LastEditors: your name
 * @LastEditTime: 2021-04-02 19:00:37
 * @Description: file content
-->

<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ isNew ? "新建文件夹" : "编辑文件夹" }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <el-form class="pl30 pr30" :model="formData" ref="form" label-width="120px">
      <el-form-item
        label="文件夹名称"
        prop="name"
        :rules="[
          { required: true, message: '文件夹名称不能为空', trigger: 'change' },
        ]"
      >
        <el-input
          v-model="formData.name"
          style="width: 400px"
          placeholder="请输入模型文件夹"
          :maxlength="64"
          show-word-limit
        ></el-input>
      </el-form-item>

      <el-form-item label="文件夹描述">
        <el-input
          type="textarea"
          v-model="formData.description"
          style="width: 400px"
          placeholder="请输入文件夹描述"
          :maxlength="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { postHmmodelfolder,putHmmodelfolder } from '@/apis/paas/HmModelFolderController.js'

export default {
  data() {
    return {
      showVisible: false,
      formData: {},
      callback: () => {},
      validator: (rule, value, callback) => {
        callback();
      },
    };
  },

  computed: {
    isNew() {
      return !this.formData.id;
    },
  },

  methods: {
    addForm(record = {}) {
      this.formData = { ...record };
      return this.showModel();
    },
    editForm(record) {
      this.formData = { ...record };
      return this.showModel();
    },
    showModel() {
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.form.validate();
        const { id, name, description } = this.formData;
        let params = {};
        let request = null;
        if (this.isNew) {
          params = { name, description };
          request = postHmmodelfolder;
        } else {
          params = { id, name, description };
          request = putHmmodelfolder;
        }
        for (const key in params) {
          params[key] =
            typeof params[key] === "string" ? params[key].trim() : params[key];
        }
        request(params).then((res) => {
          this.showVisible = false;
          this.callback();
        });
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
